package com.hxkj.service.impl;

import com.hxkj.client.service.ArticleServiceClient;
import com.hxkj.client.service.WebsiteAddrServiceClient;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.WebsiteAddr;
import com.hxkj.entity.vo.ArticleVO;
import com.hxkj.service.IndexService;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultDataStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IndexServiceImpl implements IndexService{

    @Autowired
    private ArticleServiceClient articleServiceClient;
    @Autowired
    private WebsiteAddrServiceClient websiteAddrServiceClient;

    @Override
    public List<ArticleVO> selectArticleForUserCenter() throws Exception {
        ResultDataStruct<List<Article>> result = articleServiceClient.selectArticleForUserCenter();
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        List<Article> list =  result.getData();
        List<ArticleVO> volist = new ArrayList<>();
        for (Article article : list) {
            if(article.getSiteId()!=null){
                ArticleVO vo = new ArticleVO();
                WebsiteAddr entity = new WebsiteAddr();
                entity.setSiteId(article.getSiteId());
                List<WebsiteAddr> weblist = websiteAddrServiceClient.getList(entity);
                if(weblist != null && weblist.size()>0){
                    entity = weblist.get(0);
                    vo.setArticleId(article.getArticleId());
                    vo.setHeadline(article.getHeadline());
                    //处理生产环境，不同站点文章url补全
                    vo.setManuscriptPath("http://"+entity.getIpAdd()+article.getManuscriptPath());
                    vo.setSiteId(article.getSiteId());
                    vo.setUrlPrefix(entity.getUrlPrefix());
                    vo.setIpAdd(entity.getIpAdd());
                    vo.setIpPort(entity.getIpPort());
                    volist.add(vo);
                }
            }
        }
        return volist;
    }
}
